﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 医嘱摆药申退查询 : Form
    {
        public 医嘱摆药申退查询()
        {
            InitializeComponent();
        }

        private void tSButton_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

       

        private void tSButton_cx_Click(object sender, EventArgs e)
        {
            string ZYNo = tb_ZYNo.Text.Trim();

            if (ZYNo == "")
            {
                MessageBox.Show("请输入要查询的内容！");
                tb_ZYNo.Focus();


                return;
            }


            string Hos = "select * from InHospital,Patient,Bed where Patient.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";


            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SqlCommand cmd = new SqlCommand(Hos, conn);


                if (ZYNo != "")
                {
                    SqlDataReader dr = cmd.ExecuteReader();

                    if (dr.HasRows)
                    {
                        dr.Read();

                        int nf = Convert.ToInt32(dr["BronYear"].ToString());
                        DateTime time = System.DateTime.Now;
                        int year = time.Year;
                        int nl = year - nf;
                        tb_nl.Text = nl.ToString();

                        tb_xb.Text = dr["PatSex"].ToString();
                        tb_xm.Text = dr["PatName"].ToString();
                        tb_ch.Text = dr["BedNo"].ToString();

                        tb_ye.Text = dr["HosBalance"].ToString();


                        string dno = dr["DocNo"].ToString();

                        dr.Close();

                        string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";

                        SqlCommand D = new SqlCommand(DNo, conn);

                        string DD = Convert.ToString(D.ExecuteScalar());

                        tb_jzys.Text = DD;

                    }
                    conn.Close();
                }
            }

            string WarContent = "select WarContent,MedDanwei,MedModel,MedAmount,Medscalar ,ApplicationDate,RefundCondition from ConsumptionSum where HosNo='" + tb_ZYNo.Text.Trim() + "'and  (RefundCondition='已退款'or RefundCondition='申请退款' )";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {


                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SqlCommand comContent = new SqlCommand(WarContent, conn);
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da = new SqlDataAdapter(WarContent, conn);
                ds = new DataSet();
                da.Fill(ds);

                dataGridView1.DataSource = ds.Tables[0];


            } 

        }

        private void tb_ZYNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string ZYNo = tb_ZYNo.Text.Trim();

                if (ZYNo == "")
                {
                    MessageBox.Show("请输入要查询的内容！");
                    tb_ZYNo.Focus();


                    return;
                }


                string Hos = "select * from InHospital,Patient,Bed where Patient.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";


                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {

                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    SqlCommand cmd = new SqlCommand(Hos, conn);


                    if (ZYNo != "")
                    {
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.HasRows)
                        {
                            dr.Read();

                            int nf = Convert.ToInt32(dr["BronYear"].ToString());
                            DateTime time = System.DateTime.Now;
                            int year = time.Year;
                            int nl = year - nf;
                            tb_nl.Text = nl.ToString();

                            tb_xb.Text = dr["PatSex"].ToString();
                            tb_xm.Text = dr["PatName"].ToString();
                            tb_ch.Text = dr["BedNo"].ToString();

                            tb_ye.Text = dr["HosBalance"].ToString();


                            string dno = dr["DocNo"].ToString();

                            dr.Close();

                            string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";

                            SqlCommand D = new SqlCommand(DNo, conn);

                            string DD = Convert.ToString(D.ExecuteScalar());

                            tb_jzys.Text = DD;

                        }
                        conn.Close();
                    }
                }

                string WarContent = "select WarContent,MedDanwei,MedModel,MedAmount,Medscalar ,ApplicationDate,RefundCondition from ConsumptionSum where HosNo='" + tb_ZYNo.Text.Trim() + "'and  (RefundCondition='已退款'or RefundCondition='申请退款' )";
                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {


                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    SqlCommand comContent = new SqlCommand(WarContent, conn);
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                    da = new SqlDataAdapter(WarContent, conn);
                    ds = new DataSet();
                    da.Fill(ds);

                    dataGridView1.DataSource = ds.Tables[0];


                }
            }
        }

        private void tSButton_sc_Click(object sender, EventArgs e)
        {
            tb_xb.Text = "";
            tb_nl.Text = "";
            tb_xm.Text = "";
            tb_ch.Text = "";
            tb_ye.Text = "";
            tb_ZYNo.Text = "";
            tb_jzys.Text = "";
            DataTable dt = (DataTable)dataGridView1.DataSource;
            dt.Rows.Clear();
            dataGridView1.DataSource = dt;
        }

        private void tSButton_sx_Click(object sender, EventArgs e)
        {
            string ZYNo = tb_ZYNo.Text.Trim();
            string Hos = "select * from InHospital,Patient,Bed where Patient.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";


            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SqlCommand cmd = new SqlCommand(Hos, conn);


                if (ZYNo != "")
                {
                    SqlDataReader dr = cmd.ExecuteReader();

                    if (dr.HasRows)
                    {
                        dr.Read();

                        int nf = Convert.ToInt32(dr["BronYear"].ToString());
                        DateTime time = System.DateTime.Now;
                        int year = time.Year;
                        int nl = year - nf;
                        tb_nl.Text = nl.ToString();

                        tb_xb.Text = dr["PatSex"].ToString();
                        tb_xm.Text = dr["PatName"].ToString();
                        tb_ch.Text = dr["BedNo"].ToString();

                        tb_ye.Text = dr["HosBalance"].ToString();


                        string dno = dr["DocNo"].ToString();

                        dr.Close();

                        string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";

                        SqlCommand D = new SqlCommand(DNo, conn);

                        string DD = Convert.ToString(D.ExecuteScalar());

                        tb_jzys.Text = DD;

                    }
                    conn.Close();
                }
            }
            string WarContent = "select WarContent,MedDanwei,MedModel,MedAmount,Medscalar ,ApplicationDate,RefundCondition from ConsumptionSum where HosNo='" + tb_ZYNo.Text.Trim() + "'and  (RefundCondition='已退款'or RefundCondition='申请退款' )";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {


                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SqlCommand comContent = new SqlCommand(WarContent, conn);
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds = new DataSet();
                da = new SqlDataAdapter(WarContent, conn);
                ds = new DataSet();
                da.Fill(ds);

                dataGridView1.DataSource = ds.Tables[0];


            }
            
        }
        
    }
}
